package wnhz.com.business.rebateshop.fragment;

import android.os.Build;
import android.os.Bundle;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import wnhz.com.business.R;
import wnhz.com.business.base.BaseRVAdapter;
import wnhz.com.business.base.BaseViewHolder;
import wnhz.com.business.base.MyBaseActivity;
import wnhz.com.business.base.MyBaseFragment;
import wnhz.com.business.groupshop.ui.PayActivity;
import wnhz.com.business.impl.ActionBarClickListener;
import wnhz.com.business.uitls.ToastUtils;
import wnhz.com.business.view.TranslucentActionBar;

/**
 * Created by lizhenglong on 2017-09-18 0018.
 * 购物车模块
 */

public class CarFragment extends MyBaseFragment implements ActionBarClickListener {

    @BindView(R.id.actionbar)
    TranslucentActionBar actionbar;
    @BindView(R.id.recycler_car)
    RecyclerView recyclerCar;
    @BindView(R.id.tv_txt)
    TextView tvTxt;
    @BindView(R.id.tv_number)
    TextView tvNumber;
    @BindView(R.id.tv_makepay)
    TextView tvMakepay;
    Unbinder unbinder;
    private MyBaseActivity activity;

    private int a = 0;
    private List<String> dada = new ArrayList<>();
    private int number = 1;

    @Override
    protected void handleMsg(Message msg) {


        switch (msg.what) {
            case 3001:
                actionbar.setData("购物车", 0, null, 0, "完成", 0, this);
                tvTxt.setVisibility(View.INVISIBLE);
                tvNumber.setVisibility(View.INVISIBLE);
                tvMakepay.setText("删除");
                tvMakepay.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Log.e("test0920","哈哈哈");
                    }
                });
                setdeletedata();
                break;
            case 3002:
                actionbar.setData("购物车", 0, null, 0, "编辑", 0, this);
                tvTxt.setVisibility(View.VISIBLE);
                tvNumber.setVisibility(View.VISIBLE);
                tvMakepay.setText("确定支付");
                tvMakepay.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        startActivity(PayActivity.createIntent(activity, "", "", ""));
                    }
                });
                setdata();
                break;
        }
    }

    @Override
    protected View initView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_rebate_car, container, false);
        ButterKnife.bind(this, view);
        activity = (MyBaseActivity) getActivity();
        actionbar.setData("购物车", 0, null, 0, "编辑", 0, this);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            actionbar.setStatusBarHeight(getStatusBarHeight());
        }

        initview();
        setdata();
        handler.sendEmptyMessage(3002);
        return view;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    private void initview()
    {
        recyclerCar.setLayoutManager(new LinearLayoutManager(getContext(),LinearLayoutManager.VERTICAL,false));
        dada.clear();
        for (int i = 0; i < 2; i++) {
            dada.add("a"+i);
        }
    }

    private void setdata()
    {
        recyclerCar.setAdapter(new BaseRVAdapter(getContext(),dada) {
            @Override
            public int getLayoutId(int viewType) {
                return R.layout.item_car;
            }

            @Override
            public void onBind(BaseViewHolder holder, int position) {

            }

        });
    }

    private void setdeletedata(){
        recyclerCar.setAdapter(new BaseRVAdapter(getContext(),dada) {
            @Override
            public int getLayoutId(int viewType) {
                return R.layout.item_cardelete;
            }

            @Override
            public void onBind(final BaseViewHolder holder, int position) {
                holder.getImageView(R.id.tv_add).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        number = number+1;
                        holder.getTextView(R.id.tv_numbers).setText("×"+String.valueOf(number));
                    }
                });

                holder.getImageView(R.id.tv_jian).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        number = number-1;
                        if (number<1)
                        {
                            ToastUtils.showToast(activity,"已经为最少数量");
                            number = 1;
                        }else
                            {
                                holder.getTextView(R.id.tv_numbers).setText("×"+String.valueOf(number));
                            }
                    }
                });
            }

        });
    }

    @Override
    public void onLeftClick() {

    }

    @Override
    public void onRightClick() {
        if (a % 2 == 0) {
            handler.sendEmptyMessage(3001);
            a = 1;
        } else {
            handler.sendEmptyMessage(3002);
            a = 0;
        }
    }

    @Override
    public void onEditTextClick() {

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
